////////////////////////////////////////////////////////////////////////////////////////////////////
/// \file		SDevice\SDeviceE8557D.cpp
/// 		
/// \author		Sylvain Martin
/// \version	1.0
/// \date		18/04/2011
///
/// \brief	Implants the SDeviceE8257D class
/// 		
/// \section LICENSE
///
/// This program is free software; you can redistribute it and/or
/// modify it under the terms of the GNU General Public License as
/// published by the Free Software Foundation; either version 2 of
/// the License, or (at your option) any later version.
/// 
/// This program is distributed in the hope that it will be useful, but
/// WITHOUT ANY WARRANTY; without even the implied warranty of
/// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
/// General Public License for more details at
/// http://www.gnu.org/copyleft/gpl.html
////////////////////////////////////////////////////////////////////////////////////////////////////

#include "SDevice/SDevicePSGE8257D.h"
#include "..\SError.h"
#include "SDeviceError.h"
#include "SDeviceGpib.h"

#include <qdebug.h>
#include <QtCore>

#define GHZ 1000000000
#define MHZ 1000000
#define KHZ 1000

SDevicePSGE8257D::SDevicePSGE8257D(QObject *parent):
SDeviceMicroWaveGenerator(parent)
{
	
	powerLimit_ =0.0;
	init();
}
////////////////////////////////////////////////////////////////////////////////////////////////////
/// \fn	void SDevicePSGE8257D::init(void)
///
/// \brief	Initializes the PSG E8257D device. 
///
/// this function is automatically call at the initialization of this object
////////////////////////////////////////////////////////////////////////////////////////////////////

void SDevicePSGE8257D::init(void){
	try{
		gpib_ =  SInterfaceGpib::instance();
		elmt_= gpib_->findGpibElement(QString("Agilent Technologies, E8257D"));

		if (!(elmt_.PAD ==-1)) available_ = true;


	}catch(SError error){
		if (!(error == SDEVICEERROR_GPIB_FINDLSTN)){
			SError::traitement(this,error);	
		}else{

			this->setUnavailable();
			//qDebug() << "\nAgilent PSGE8257D not found.\n";
		}

	}
}
////////////////////////////////////////////////////////////////////////////////////////////////////
/// \fn	void SDevicePSAE4440A::setFreqStart(double const freq)
///
/// \brief	(SCRIPTABLE) Sets the frequency of the microwave. 
///
/// \param	freq The frequency in GHz. 
///
////////////////////////////////////////////////////////////////////////////////////////////////////
void SDevicePSGE8257D::setFrequence(double const freq){
	frequency_ = freq;

	QString cmd;

	try{

		if (freq< 1/KHZ){
			cmd = QString(":FREQ ") + QString::number(freq, 'f') + QString(" GHZ");
		}else{ // for the small RBW, avoid the cmd == 0.000000 GHZ
			cmd = QString(":FREQ ") + QString::number(freq*GHZ, 'f') + QString(" HZ");
		}

		gpibSend(cmd);
	}catch(SError error){
		SError::traitement(this,error);
	}
}

////////////////////////////////////////////////////////////////////////////////////////////////////
/// \fn	void SDevicePSAE4440A::setFreqStart(double const freq)
///
/// \brief	(SCRIPTABLE) Sets the power of the microwave. 
///
/// \param	pwr The power in dBm. 
///
////////////////////////////////////////////////////////////////////////////////////////////////////
void SDevicePSGE8257D::setPower(double const pwr){

	if (pwr >= -135 && pwr <= powerLimit_){

		QString cmd;

		try{
			cmd = QString(":POW ") + QString::number(pwr) + QString(" DBM");
			gpibSend(cmd);
		}catch(SError error){
			SError::traitement(this,error);
		}


		power_ = pwr;
	}
}

////////////////////////////////////////////////////////////////////////////////////////////////////
/// \fn	void SDevicePSAE4440A::setFreqStart(double const freq)
///
/// \brief	(SCRIPTABLE) Sets the power limit of the microwave. 
///
/// \param	pwr The power in dBm. 
///
////////////////////////////////////////////////////////////////////////////////////////////////////
void SDevicePSGE8257D::setPowerLimit(const double pwrLim){
	powerLimit_ = pwrLim; 
}
////////////////////////////////////////////////////////////////////////////////////////////////////
/// \fn	void SDevicePSAE4440A::setFreqStart(double const freq)
///
/// \brief	(SCRIPTABLE) activates or deactivates the microwave. 
///
/// \param	OnOff  activate if true, deactivates otherwise. 
///
////////////////////////////////////////////////////////////////////////////////////////////////////
void SDevicePSGE8257D::activate(bool OnOff){
	activated_ = OnOff;

	QString cmd;

	try{
		if(OnOff){
			cmd = QString(":OUTP ON");
		}else{
			cmd = QString(":OUTP OFF");
		}
		
		gpibSend(cmd);
	}catch(SError error){
		SError::traitement(this,error);
	}
}

////////////////////////////////////////////////////////////////////////////////////////////////////
/// \fn	 QString SDevicePSGE8257D::description(void) const
///
/// \brief	(SCRIPTABLE) Gets a brief description of the device. 
///
/// \return	a description of the device. 
////////////////////////////////////////////////////////////////////////////////////////////////////

QString SDevicePSGE8257D::description(void) const
{
	
	QString des	=  "The Agilent E8257D is a fully synthesized signal generator with high output";
	des	+= "power, low phase noise, and optional ramp sweep capability";


	return des;
}
////////////////////////////////////////////////////////////////////////////////////////////////////
/// \fn	bool SDevicePSGE8257D::available(void)
///
/// \brief	(SCRIPTABLE) Gets the availability. 
///
/// \return	true if the device is available, false if it is not. 
////////////////////////////////////////////////////////////////////////////////////////////////////
bool SDevicePSGE8257D::available( void ){

	try{
		gpib_ =  SInterfaceGpib::instance();
		elmt_= gpib_->findGpibElement(QString("Agilent Technologies, E8257D"));

		available_ = (elmt_.PAD !=-1);

	}catch(SError error){
		if (!(error == SDEVICEERROR_GPIB_FINDLSTN)){
			SError::traitement(this,error);	
		}else{

			this->setUnavailable();
			//qDebug() << "\nWARNING : We must turn on, at least, one device to use the GPIB\n";
		}

	}


	return available_;
}

